{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "197e9a1b-f3d6-4ea0-9b07-15c0b75022df",
   "metadata": {},
   "outputs": [],
   "source": [
    "class ReferenceFrame:\n",
    "    def __init__(self, velocity=0):\n",
    "        self.v = velocity  # 速度，以光速 c 为单位\n",
    "        self.gamma = 1 / sqrt(1 - velocity**2) if velocity != 1 else infinity  # 洛伦兹因子\n",
    "\n",
    "    def relative_velocity(self, other):\n",
    "        \"\"\"计算另一参考系相对于当前参考系的相对速度\"\"\"\n",
    "        return (other.v - self.v) / (1 - self.v * other.v)\n",
    "\n",
    "    def lorentz_transform(self, t, x, other):\n",
    "        \"\"\"将事件 (t, x) 从当前参考系变换到另一参考系，返回 (t', x')\"\"\"\n",
    "        u = self.relative_velocity(other)\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        t_prime = gamma * (t - u * x)\n",
    "        x_prime = gamma * (x - u * t)\n",
    "        return (t_prime, x_prime)\n",
    "\n",
    "    def time_dilation(self, proper_time, other):\n",
    "        \"\"\"计算原时 (当前系) 在另一参考系中的时间膨胀\"\"\"\n",
    "        u = abs(self.relative_velocity(other))  # 速度取绝对值\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        return gamma * proper_time\n",
    "\n",
    "    def length_contraction(self, proper_length, other):\n",
    "        \"\"\"计算原长 (当前系) 在另一参考系中的长度收缩\"\"\"\n",
    "        u = abs(self.relative_velocity(other))  # 速度取绝对值\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        return proper_length / gamma\n",
    "\n",
    "    def velocity_addition(self, w, other):\n",
    "        \"\"\"将当前参考系中的速度 w 转换为另一参考系中的速度\"\"\"\n",
    "        u = self.relative_velocity(other)\n",
    "        return (w - u) / (1 - w * u)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c0dc9a8-ee92-45ed-94ce-b828f713ce5f",
   "metadata": {},
   "source": [
    "近代物理作业 一周 2.4 移动的星系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1f6d1c9f-a580-426c-b662-c7a8b348570c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.25000000000000e10\n"
     ]
    }
   ],
   "source": [
    "# 第一问\n",
    "S1 = ReferenceFrame(0)\n",
    "S2 = ReferenceFrame(0.6)\n",
    "S2_time=10e9 # 固有时\n",
    "S1_time = S2.time_dilation(S2_time,S1) \n",
    "# S2的时间，在S1眼里如何\n",
    "# 如果换种写法，那就是，在S2系里，这个来自S1的时间多少\n",
    "# 当前这种写法里，第一个值是此对象自己系里的\n",
    "print(S1_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fcefc912-70df-4f68-ad03-58f3ca5d1070",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.05000000000000e10\n",
      "7.80000000000000e9\n"
     ]
    }
   ],
   "source": [
    "# 第二问 S2死亡时离S1多远？\n",
    "# 估计是以S1系中的距离\n",
    "# 两种算法，一种是以S1系的时间加上速度，直接得到\n",
    "# 另一种是以S2系中移动的距离，但是在S1系中尺缩了\n",
    "\n",
    "# 法1\n",
    "print(3e9+S1_time*0.6)\n",
    "\n",
    "# 法2？\n",
    "print(3e9+ S2.length_contraction(S2_time*0.6,S1))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d69f6b68-d9f6-45b7-8bc3-a5b694cb3367",
   "metadata": {},
   "source": [
    "看起来有点问题……因为S2系中并不知道自己以0.6c速度在移动吧？\n",
    "实际上S2眼里只有S1在后退的样子……它眼里的速度不见得是0.6c吧\n",
    "然后它眼里的3e9距离，恐怕也不是那些距离？\n",
    "总之……方法2目前是有问题的……先以方法1简单为准吧\n",
    "至于具体要使用法2怎么操作，只有日后熟悉了再重新来看了\n",
    "翻了一下答案，法1得出的答案是对的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cd4f7f7c-68d7-4ef9-91f3-df0f7b9853b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.00000000000000e10\n"
     ]
    }
   ],
   "source": [
    "# 第三问 ：S1接受S2的光持续多久？\n",
    "# 先从那个地方……再到那个地方……有点像追击问题？\n",
    "# 感觉可以忽略掉初始的3e9距离吧，\n",
    "# S2照S2初始位置的光，时间从0到S2消失后有段时间\n",
    "# 所以时间是 t+尺缩的这个距离除以光速\n",
    "print(S1_time + S1_time*0.6/1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8307708-43be-44f3-922a-27ca1bd137fe",
   "metadata": {},
   "source": [
    "2.5 电子与管道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4cba7ab6-6006-4008-b143-f3a1fa40e8d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00699999997238472\n"
     ]
    }
   ],
   "source": [
    "# 只有一个问，还算可以\n",
    "# 不过这个速度值有点离谱\n",
    "# 2.4主要是钟慢的公式，电子这里是尺缩了，很明显\n",
    "S1=ReferenceFrame(0)\n",
    "S2 = ReferenceFrame(1-2.45e-9)\n",
    "print(S1.length_contraction(100,S2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e09221cd-5c4a-4f7c-84aa-a5495f187882",
   "metadata": {},
   "source": [
    "2.7 粒子分裂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3d38715c-3b9a-4270-85de-60808461e78e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.953749498730116\n",
      "-0.450575625248114\n"
     ]
    }
   ],
   "source": [
    "# 只有一问，问这种分裂出的粒子的最大速度\n",
    "# 这个就确确实实的是，S2里的速度，在S1眼里如何了\n",
    "S1 = ReferenceFrame(0)\n",
    "S2 = ReferenceFrame(0.6) #又是0.6……因为1.25是挺整数的吧\n",
    "print(S2.velocity_addition(0.827,S1))\n",
    "# 反方向也看看\n",
    "print(S2.velocity_addition(-0.827,S1))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "bc6eb30d-85c8-430b-9591-35b093fec68b",
   "metadata": {},
   "source": [
    "程序简化↓"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.5",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
